package com.tianzj.mvc.system.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianzj.mvc.system.dao.SysMenuMapper;
import com.tianzj.mvc.system.dao.SysRoleMenuMapper;
import com.tianzj.mvc.system.pojo.SysMenu;
import com.tianzj.mvc.system.pojo.SysMenuExample;
import com.tianzj.mvc.system.service.SysMenuService;
import com.tianzj.util.SnowflakeIdWorker;
import com.tianzj.util.tree.BuildTree;
import com.tianzj.util.tree.Tree;

@Service
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;
	
	public SysMenuExample getExample(SysMenu sysMenu){
		SysMenuExample example=new SysMenuExample();
		SysMenuExample.Criteria criteria = example.createCriteria();
		if(sysMenu.getId()!=null){
			criteria.andIdEqualTo(sysMenu.getId());
		}
		if(sysMenu.getStatus()!=null){
			criteria.andStatusEqualTo(sysMenu.getStatus());
		}
		return example;
	}
	
	@Override
	public void saveSysMenu(SysMenu sysMenu) {
		sysMenu.setId(new SnowflakeIdWorker(0, 0).nextId());
		sysMenuMapper.insertSelective(sysMenu);
		
	}

	@Override
	public void updateSysMenu(SysMenu sysMenu) {
		sysMenuMapper.updateByPrimaryKey(sysMenu);
	}

	@Override
	public List<SysMenu> getSysMenuList() {
		SysMenuExample example=new SysMenuExample();
		SysMenuExample.Criteria criteria = example.createCriteria();
	    criteria.andStatusNotEqualTo(9);
		return sysMenuMapper.selectByExample(example);
	}

	@Override
	public SysMenu findById(Long id) {
		return sysMenuMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SysMenu> findByParentId(Long id) {
		SysMenuExample example=new SysMenuExample();
		SysMenuExample.Criteria criteria = example.createCriteria();
	    criteria.andStatusNotEqualTo(9);
	    criteria.andParentIdEqualTo(id);
		return sysMenuMapper.selectByExample(example);
	}

	@Override
	public void deleteSysMenuById(Long id) {
		sysMenuMapper.deleteByPrimaryKey(id);
	}

	@Override
	public Tree<SysMenu> getTree(Long id) {
		//查询所有的权限菜单不是删除状态和暂存状态的菜单
		List<SysMenu> sysMenus = findByStatusEquals(1);
		//获取该角色所拥有的权限caidan
		List<Long> menuIds = sysRoleMenuMapper.findMenuIdByRoleId(id);
		List<Long> temp = menuIds;
		for (SysMenu menu : sysMenus) {
			if (temp!=null&&temp.size()>0&&temp.contains(menu.getParentId())) {
				menuIds.remove(menu.getParentId());
			}
		}
		List<Tree<SysMenu>> trees = new ArrayList<Tree<SysMenu>>();
		//List<SysMenu> SysMenus =getMeunsList();
		for (SysMenu menu : sysMenus) {
			Tree<SysMenu> tree = new Tree<SysMenu>();
			tree.setId(menu.getId().toString());
			tree.setParentId(menu.getParentId().toString());
			tree.setText(menu.getName());
			Map<String, Object> state = new HashMap<String,Object>();
			Long menuId = menu.getId();
			if (menuIds!=null&&menuIds.size()>0&&menuIds.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<SysMenu> t = BuildTree.build(trees);
		return t;
	}

	private List<SysMenu> findByStatusEquals(int i) {
		SysMenuExample example=new SysMenuExample();
		SysMenuExample.Criteria criteria = example.createCriteria();
	    criteria.andStatusEqualTo(i);
		return sysMenuMapper.selectByExample(example);
	}

	@Override
	public SysMenu getMenuById(Long menuId) {
		return sysMenuMapper.selectByPrimaryKey(menuId);
	}


}
